import express from 'express';
import cors from 'cors';
import dataRoutes from './src/routes/dataRoutes.js';
import swaggerUi from 'swagger-ui-express';
import swaggerJSDoc from 'swagger-jsdoc';
import apiDailyStatsRoutes from './src/routes/apiDailyStatsRoute.js'
const app = express();

// 配置CORS
const corsOptions = {
  origin: true, // 允许所有来源
  methods: ['GET', 'POST', 'PUT', 'DELETE'], // 允许的HTTP方法
  allowedHeaders: ['Content-Type', 'Authorization'], // 允许的请求头
  credentials: true // 允许携带凭证
};

// 基础中间件
app.use(cors(corsOptions));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// API路由
app.use('/api', dataRoutes);

// Swagger配置
const swaggerOptions = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: 'Mock API Service',
      version: '1.0.0',
      description: 'Mock数据接口文档'
    },
    servers: [{ url: '/api' }]
  },
  apis: ['./src/routes/*.js'] // 确保路径正确
};

const swaggerSpec = swaggerJSDoc(swaggerOptions);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));

// 健康检查接口
app.get('/health', (req, res) => {
  res.status(200).json({ status: 'ok' });
});

// 添加端口配置
const PORT = 4000;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

export default app;